package redis_init

import (
	"github.com/go-redis/redis"
	"sync"
)

import (
	"fmt"
)

//
//1.单例创建Map

var (
	once sync.Once
)

//func New() singleton {
//	once.Do(func() {
//		instance = make(singleton)
//	})
//
//	return instance
//}

type RedisDBs struct {
	RedisDB *redis.Client
}

//
//2.单例创建struct
type RedisConfig struct {
	ip   string
	port int
	auth string
}

var (
	once2 sync.Once
)

func (c *RedisDBs) GetRedisInstance() {
	once2.Do(func() {
		//mock read from file

		fmt.Println("Create RedisConfig")
		c.RedisDB = redis.NewClient(&redis.Options{
			Addr:     "120.25.73.138:6379",
			Password: "",
			DB:       0,
		})
	})
}

//
////测试函数
//func TestCreateMapSingleton() {
//	fmt.Println("++++++TestCreateMapSingleton beg")
//	s := New()
//	s["this"] = "that"
//	s2 := New()
//	fmt.Println("This is ", s2["this"])
//	fmt.Println("++++++TestCreateMapSingleton end")
//
//}
//
//func TestCreateStructSingleton() {
//	fmt.Println("++++++TestCreateStructSingleton beg")
//	var wg sync.WaitGroup
//	for i := 0; i < 3; i++ {
//		wg.Add(1)
//		go func() {
//			obj := GetRedisInstance()
//			fmt.Printf("%X\n", unsafe.Pointer(obj))
//			fmt.Println(*redisInstance)
//			wg.Done()
//		}()
//	}
//	wg.Wait()
//	fmt.Println("++++++TestCreateStructSingleton end")
//
//}
//
////
//
//func main() {
//	TestCreateMapSingleton()
//	TestCreateStructSingleton()
//}
